__author__ = 'Administrator'
#coding=utf-8
import wx
#from wxPython.lib.editor import images
from wx.lib.embeddedimage import PyEmbeddedImage
import pic
import images
import pyPopWin
import thread

class MyFrame(wx.Frame):
    def __init__(self, parent):
        wx.Frame.__init__(self, parent, -1, "Shaped Window",pos=(1000, 200),
                         style = wx.FRAME_SHAPED | wx.SIMPLE_BORDER | wx.FRAME_NO_TASKBAR | wx.STAY_ON_TOP
                         | wx.TRANSPARENT_WINDOW
                         )
        self.hasShape = False
        self.delta = (0,0)
        self.isShowPopWin = True

#        self.timer = wx.Timer(self)
#        self.timer.Start(10000)

        ## 创建右键弹出式菜单
        self.popupmenu = wx.Menu()#创建一个菜单
        for text in "Close Exit".split():#填充菜单
            item = self.popupmenu.Append(-1, text)
            if text == 'Exit':
                self.Bind(wx.EVT_MENU, self.OnExit, item)
            elif text == 'Close':
                self.Bind(wx.EVT_MENU, self.ClosePopWin, item)
        self.Bind(wx.EVT_CONTEXT_MENU, self.OnShowPopup)#绑定一个显示菜单事件

        self.Bind(wx.EVT_LEFT_DCLICK,   self.OnDoubleClick)
        self.Bind(wx.EVT_LEFT_DOWN,     self.OnLeftDown)
        self.Bind(wx.EVT_LEFT_UP,       self.OnLeftUp)
        self.Bind(wx.EVT_MOTION,        self.OnMouseMove)
        self.Bind(wx.EVT_PAINT,         self.OnPaint)
#        self.Bind(wx.EVT_TIMER,         self.OnTimer)

        self.bmp = PyEmbeddedImage(pic.pic1).GetBitmap()
        w, h = self.bmp.GetWidth(), self.bmp.GetHeight()
        self.SetClientSize( (w, h) )

        if wx.Platform != "__WXMAC__":
            # wxMac clips the tooltip to the window shape, YUCK!!!
            self.SetToolTipString("Right-click to close the window\n"
                                  "Double-click the image to set/unset the window shape")

        if wx.Platform == "__WXGTK__":
            # wxGTK requires that the window be created before you can
            # set its shape, so delay the call to SetWindowShape until
            # this event.
            self.Bind(wx.EVT_WINDOW_CREATE, self.SetWindowShape)
        else:
            # On wxMSW and wxMac the window has already been created, so go for it.
            self.SetWindowShape()

        dc = wx.ClientDC(self)
        dc.DrawBitmap(self.bmp, 0,0, True)

    def OnShowPopup(self, event):#弹出显示
         pos = event.GetPosition()
         pos = self.ScreenToClient(pos)
         self.PopupMenu(self.popupmenu, pos)

    def SetWindowShape(self, *evt):
        # Use the bitmap's mask to determine the region
        r = wx.RegionFromBitmap(self.bmp)
        self.hasShape = self.SetShape(r)


    def OnDoubleClick(self, evt):
        if self.hasShape:
            self.SetShape(wx.Region())
            self.hasShape = False
        else:
            self.SetWindowShape()

    def OnPaint(self, evt):
        dc = wx.PaintDC(self)
        dc.DrawBitmap(self.bmp, 0,0, True)

    def OnExit(self, evt):
        self.isShowPopWin = False
        pyPopWin.Exit()
        self.Close()

    def OnLeftDown(self, evt):
        self.CaptureMouse()
        x, y = self.ClientToScreen(evt.GetPosition())
        originx, originy = self.GetPosition()
        dx = x - originx
        dy = y - originy
        self.delta = ((dx, dy))

    def OnLeftUp(self, evt):
        if self.HasCapture():
            self.ReleaseMouse()

        self.ShowPopWin()
        self.isShowPopWin = True


    def OnMouseMove(self, evt):
        if evt.Dragging() and evt.LeftIsDown():
            x, y = self.ClientToScreen(evt.GetPosition())
            fp = (x - self.delta[0], y - self.delta[1])
            self.Move(fp)
            # 如果移动就步显示弹出式窗口
            self.isShowPopWin = False

#    def OnTimer(self, evt):
#        if self.popWinIsShow:
#            self.ShowPopWin()
        
    def ShowPopWin(self):
        if not self.isShowPopWin:
            return
        if not pyPopWin.isShow():
            pyPopWin.Show(True)
        else:
            pyPopWin.Show(False)

    def ClosePopWin(self, evt):
        pyPopWin.Exit()
        
if __name__ == "__main__":
    app = wx.PySimpleApp()
    frame = MyFrame(None)
    frame.Show(True)
    app.MainLoop()
    print 'pyLnkManage: Destroy'
    